ADD TO STACK

This command will add a blank item to the array stack.

  Syntax
ADD TO STACK Array Name(0)
  Parameters
Array Name(0
Integer
The name of the array followed by a pair of brackets ( ). You can also insert a value of zero, i.e. arrayname(0)

  Returns

This command does not return a value.

  Description

Stacks are a first in, last out data structure. You can imagine a stack as a pile of books. You can only take the book that was last added to the stack. To get to the item at the start of the stack, you must first remove all other items.

  Example Code
dim a() as integer
empty array a()

print "Add 5 items to the stack"
for i=1 to 5
n = rnd(1000)
print " Adding "; n; " to the stack"
add to stack a()
a() = n
next i

print
PrintStack()
print
print "Pop 2 items from the stack"

for i=1 to 2
print " Removing "; a(); " from the stack"
remove from stack a()
next i

print
PrintStack()
print

print "Add 5 more entries to the stack"
for i=0 to 4
n = rnd(1000)
print " Adding "; n; " to the stack"

add to stack a()
a() = n
next i

print
PrintStack()
print

print "Now remove items until the stack is empty"
while array index valid( a() )
print " Removing "; a(); " from the stack"
remove from stack a()
endwhile

print
PrintStack()

wait key
end

function PrintStack()
if array count( a() ) >= 0
print "The stack has "; array count( a() )+1; " items"
for i=0 to array count( a() )
print " Item "; i; " = "; a(i)
next i
else
print "The stack is empty"
endif
endfunction
  See also

CORE Commands Menu
Index